
Page({
  data: {
    id: null,
    uname: '',
    month: null,
    year: null,
    dayList: [],
    datail: [],
    userid: ""
  },
  async onLoad (e) {
 
    this.setData({
      userid: e.id
    });
    wx.setNavigationBarTitle({
      title: e.uname + '的签到详情'
    })
    this.setData({
      id: parseInt(e.id),
      uname: e.uname
    });
    //获取当前
    await this.nowMonth();
    this.popup = this.selectComponent('#lkpPopup');//获取弹窗组件
  },
  async handAmend (e) {
    console.log(e)
    // throw new Error()
    wx.showModal({
      title: '提示',
      content: '确定要修改吗？',
      success: async (sm) => {
        if (sm.confirm) {
          console.log(e.currentTarget.dataset.ampm)
          let amendObj = {
            
            y: this.data.year.toString(),
            m: this.data.month.toString().length == 1 ? '0' + this.data.month.toString() : this.data.month.toString(),
            state: e.currentTarget.dataset.state,
            d: e.currentTarget.dataset.da.toString().length == 1 ? '0' + e.currentTarget.dataset.da.toString() : e.currentTarget.dataset.da.toString(),
            isPm: e.currentTarget.dataset.ispm,
            userId: this.data.userid
          }
          const data = await getApp().httpModule.setUserState(amendObj);
          if (data.statusCode == 200) {
            await this.getDayList(amendObj.y, amendObj.m)
            console.log(data, '成功吗', amendObj)
          }


        } else if (sm.cancel) {
          console.log('用户点击取消')
          wx.showToast({
            title: '取消修改',
            icon: 'none',
            duration: 1000
          })
        }
      }
    })


  },
  clickImg (e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.img],
      current: '',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  async nowMonth () {
    let month = parseInt(new Date().format('MM'))
    let year = parseInt(new Date().format('yyyy'))
    this.setData({
      month,
      year
    });
    await this.getDayList(year, month)
  },
  async lastMonth () {
    let month = this.data.month == 1 ? 12 : this.data.month - 1;
    let year = this.data.month == 1 ? this.data.year - 1 : this.data.year;
    this.setData({
      month,
      year
    });
    await this.getDayList(year, month)
  },
  //下个月
  async nextMonth () {
    let month = this.data.month == 12 ? 1 : this.data.month + 1;
    let year = this.data.month == 12 ? this.data.year + 1 : this.data.year;
    this.setData({
      month,
      year
    });
    await this.getDayList(year, month)
  },
  async getDayList (year, month) {
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: '刷新中',
    })
    let now_month = parseInt(new Date().format('MM'));
    let now_year = parseInt(new Date().format('yyyy'));
    let dayCount = new Date(parseInt(year), parseInt(month), 0).getDate();//31天
    if (now_month == month && now_year == year) {
      dayCount = parseInt(new Date().format('dd'));//当前
    }
    if (now_month < month && now_year <= year) {
      wx.showModal({
        title: '温馨提示',
        content: '未来无法预测～',
      })
      await this.nowMonth()
      wx.hideLoading();
      wx.hideNavigationBarLoading();
      throw new Error()
    }
    const dayList = [];
    const p = {
      userId: this.data.id,
      page: 1,
      limit: 100,
      time: this.data.year + '-' + this.data.month
    }
    const data = await getApp().httpModule.statisticsSignByUser(p);

    const signList = data.data.data;
    console.log(signList)
    signList.forEach(item=>{
      
      if(item.locationName){
        try {
          item.locationName = JSON.parse(item.locationName)
        } catch (error) {
          item.locationName =item.locationName
        }
       
      }
    
    })
    console.log('jitian',dayCount,signList)
    for (let i = 0; i < dayCount; i++) {
      
      let obj = {
        date: i + 1,
        am: false,
        pm: false,
        site:'暂无位置信息'
      }
      for (let u in signList) {
        //  const p = {
        //   userId: this.data.id,
        //   time: this.data.year + '-' + this.data.month + '-' + signList[u].d
        // }
      // const data = await getApp().httpModule.signInfoByUser(p);
       
      // if(data.data&&data.data.data[0]&&data.data.data[0].locationName){
      
      //   if(data.data.data[0].locationName.indexOf("县") !== -1){
      //     data.data.data[0].locationName = data.data.data[0].locationName.substring(0, data.data.data[0].locationName.indexOf('县')+1)
      //   }else if(data.data.data[0].locationName.indexOf("区") !== -1){
      //     data.data.data[0].locationName = data.data.data[0].locationName.substring(0, data.data.data[0].locationName.indexOf('区')+1)
   
      //   }else{
      //     data.data.data[0].locationName = data.data.data[0].locationName
      //   }
      
      // }
   
       function jsonSrt(str) {
        if(typeof(str) === 'string') {
            try{
                var obj = JSON.parse(str);
                if(typeof(obj) === 'object' && obj) {
                    return true;
                }
                else {
                    return false;
                }
            }
            catch(e) {
                return false;
            }
        }
    }
        if (signList[u].signIsPm == '0' && parseInt(signList[u].d) == (i + 1)) {
          obj.am = true;
          obj.pm = false;
          if(signList[u].locationName){
            try {
              obj.site =  signList[u].locationName.address_component.province+signList[u].locationName.address_component.city+signList[u].locationName.address_component.district
            } catch (error) {
              obj.site =signList[u].locationName
            }
          
          }
         
          // console.log(  JSON.parse(signList[u].locationName.address_component),'早上')
          // obj.site =JSON.parse(signList[u].locationName.address_component)
           
       
        }
        if ((signList[u].signIsPm == '0,1'||signList[u].signIsPm == '1,0') && parseInt(signList[u].d) == (i + 1)) {
          obj.am = true;
          obj.pm = true;
          try {
            obj.site =  signList[u].locationName.address_component.province+signList[u].locationName.address_component.city+signList[u].locationName.address_component.district
          } catch (error) {
            obj.site =signList[u].locationName
          }
          // if(signList[u].locationName){
          //   obj.site =  signList[u].locationName.address_component.province+signList[u].locationName.address_component.city+signList[u].locationName.address_component.district
          // }
        
         
        }
        if (signList[u].signIsPm == '1' && parseInt(signList[u].d) == (i + 1)) {
          // console.log(  JSON.parse(signList[u].locationName.address_component),'下午')
          obj.am = false;
          obj.pm = true;
          try {
            obj.site =  signList[u].locationName.address_component.province+signList[u].locationName.address_component.city+signList[u].locationName.address_component.district
          } catch (error) {
            obj.site =signList[u].locationName
          }
          // if(signList[u].locationName){
          //   obj.site =  signList[u].locationName.address_component.province+signList[u].locationName.address_component.city+signList[u].locationName.address_component.district
          // }
        
            
        }
      }
      dayList.push(obj)
    }
    this.setData({ dayList })
    console.log(signList)
    console.log(dayList)
    wx.hideLoading();
    wx.hideNavigationBarLoading();
  },
  async showDetail (e) {
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: '刷新中',
    })
    const value = e.currentTarget.dataset.detail;
   
    this.setData({
      chooseDay: value.date
    })
    
    const p = {
      userId: this.data.id,
      time: this.data.year + '-' + this.data.month + '-' + value.date
    }
    const data = await getApp().httpModule.signInfoByUser(p);
    const showDetail = data.data.data;
    const dataDetail = [{ isPm: 0 }, { isPm: 1 }];
    for (let i in dataDetail) {

      dataDetail[i].data = {}
      for (let u in showDetail) {
        if (dataDetail[i].isPm === showDetail[u].isPm) {
          dataDetail[i].data = showDetail[u]
        }
      }
    }
  dataDetail.forEach(item=>{
    console.log(item,'查看');
    if(item.data.locationName == null){
      item.data.locationName = '暂无位置信息'
    }
    if(JSON.stringify(item.data) != "{}"){
      try {
        item.data.locationName =JSON.parse(item.data.locationName)
      } catch (error) {
        item.data.locationName =item.data.locationName
      }

    }

  })
  

    this.setData({ datail: dataDetail })

   
    wx.hideLoading();
    wx.hideNavigationBarLoading();
    this.popup.open()
  },
  closepopup () {
    this.popup.close()
  }
})